from django.shortcuts import render
from .models import Medicine
import jwt
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import AllowAny
from rest_framework_simplejwt.tokens import RefreshToken


import os
from django.utils.timezone import now
from django.conf import settings
# Create your views here.


class Get_All(APIView):
    permission_classes = [AllowAny]  # 允许任何人访问

    def get(self, request):

        try:
            # 查找 Patient 实例
            medicines = Medicine.objects.all()
            medicines_data = list(medicines.values('id','name','producer','counts','price','date'))
        except Medicine.DoesNotExist:
            return Response({"error": "Medicine not found"}, status=400)

        if medicines:
            return Response({'medicines': medicines_data}, status=200)

        return Response({"error": "medicines not found"}, status=400)




class Create(APIView):
    permission_classes = [AllowAny]  # 允许任何人访问

    def post(self, request):
        medicine_name = request.data.get('name')
        medicine_producer = request.data.get('producer')
        medicine_counts = request.data.get('counts')
        medicine_price = request.data.get('price')
        medicine_date = request.data.get('date')

        if not medicine_name:
            return Response({"error": "medicine_name is required"}, status=400)
        if not medicine_producer:
            return Response({"error": "medicine_producer is required"}, status=400)
        if not medicine_counts:
            return Response({"error": "medicine_counts is required"}, status=400)
        if not medicine_price:
            return Response({"error": "medicine_price is required"}, status=400)
        if not medicine_date:
            return Response({"error": "medicine_date is required"}, status=400)

        if medicine_name and medicine_producer and medicine_counts and medicine_price and medicine_date:
            Medicine.objects.create(
                name=medicine_name,
                producer=medicine_producer,
                counts=medicine_counts,
                price=medicine_price,
                date=medicine_date
            )

            return Response({"message": "创建成功"}, status=200)

        return Response({"error": "Unsuccess"}, status=400)



# class Change(APIView):
#     permission_classes = [AllowAny]  # 允许任何人访问
#
#     def post(self, request):
#         medicine_id = request.data.get('medicine_id')
#         medicine_counts = request.data.get('medicine_counts')
#
#         try:
#
#             medical_record = Medical_record.objects.get(id=medical_record_id)
#         except Medical_record.DoesNotExist:
#             return Response({"error": "Medical_record not found"}, status=400)
#         if medical_record_id and medical_record_content and medical_record_is_completed:
#             medical_record.content = medical_record_content
#             medical_record.is_completed = medical_record_is_completed
#             medical_record.save()
#
#             return Response({"message": "Success"}, status=200)
#         return Response({"error": "Unsuccess"}, status=400)